<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>充值续费</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/js/layui/css/layui.css" media="all">
</head>
<body>
<div style="width:80%;margin:15px auto;overflow:auto;" id="aaa">
    <form class="layui-form" action="">
        <input type="hidden" class="input-text" id="uid" name="uid">
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block">
                <input type="text" name="username" required disabled lay-verify="required" placeholder="请输入用户名" autocomplete="off" class="layui-input layui-disabled">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">剩余时长(分钟)</label>
            <div class="layui-input-block">
                <input type="text" name="shenyu_time" required disabled lay-verify="required" placeholder="请输入剩余时长" autocomplete="off" class="layui-input layui-disabled">
            </div>
            <!--<div class="layui-form-mid layui-word-aux">辅助文字</div>-->
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">充值金额</label>
            <div class="layui-input-block">
                <input type="text" id="money" name="money" required lay-verify="required" placeholder="请输入充值金额" autocomplete="off" class="layui-input">
            </div>
            <!--<div class="layui-form-mid layui-word-aux">辅助文字</div>-->
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">充值时长</label>
            <div class="layui-input-inline">
                <input type="text" id="recharge_time" name="recharge_time" required disabled lay-verify="required" placeholder="" autocomplete="off" class="layui-input layui-disabled">
            </div>
            <div class="layui-input-inline" style="width: 300px;line-height: 38px;">
                <span>分钟</span>
            </div>
            <!--<div class="layui-form-mid layui-word-aux">辅助文字</div>-->
        </div>

        <div class="layui-form-item" style="margin-top: 20px">
            <div class="layui-input-block">
                <button class="layui-btn" id="addRecharge" lay-submit lay-filter="addRecharge">立即充值</button>
                <button type="reset" id="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<script src="/js/layui/layui.js"></script>
<script src="/js/layui/layui.all.js"></script>
<script src="/admin/js/token.js"></script>

<script>
    //url 传参
    function GetQueryString(name){
        var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
        var a = decodeURI(window.location.search);
        var r = a.substr(1).match(reg);//search,查询？后面的参数，并匹配正则
        if(r!=null)return  unescape(r[2]); return null;
    }

    window.onload = function(){
        $("input[name='uid']").val(GetQueryString("id"));
        $("input[name='username']").val(GetQueryString("username"));
        $("input[name='shenyu_time']").val(GetQueryString("syTime"));
    }

    $("#money").on('keyup', function () {
        var money = $("#money").val().replace(/^(0+)|[^\d.]+/g,'');
        $("#money").val(money);
        $.ajax({
            url: "/shoufei/getShoufeiBiaozhun?money=" + money
            , success: function (data) {
                if(data.code==200) {
                    $("#recharge_time").val(data.data)
                }else if(data.code==500){
                    layer.msg(data.msg);
                }
            }
            , error: function () {
                console.log("ajax error");
            }
        });
    })

    layui.use(['form' , 'jquery' , 'upload'], function(){
        var form = layui.form;
        var $ = layui.jquery;

        //修改操作
        form.on('submit(addRecharge)',function(data){
            var uid = $("#uid").val();
            data.field.uid = uid;
            $.ajax({
                url: "/shoufei/addRecharge"
                ,type:"POST"
                ,contentType: 'application/json'
                ,data:JSON.stringify(data.field)
                , success: function (data) {
                    if(data.code==500) {
                        layer.msg('充值失败！');
                    }else if(data.code==200){
                        layer.close(layer.index);
                        layer.msg('充值成功！');
                        setTimeout(function(){
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);//关闭弹出层
                            parent.location.reload();//重新加载父页面表格
                        }, 2000);
                    }
                }
                , error: function () {
                    console.log("ajax error");
                }
            });
            return false;
        });
    });
</script>
</body>
</html>